package com.cskaoyan.search.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.search.converter.ProductConverter;
import com.cskaoyan.search.entity.ItemDocument;
import com.cskaoyan.search.remote.ProductApiClient;
import com.cskaoyan.search.repository.ProductRepository;
import com.cskaoyan.search.service.InitDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    private ProductApiClient productApiClient;

    @Autowired
    private ProductConverter productConverter;

    @Autowired
    private ProductRepository productRepository;

    @Override
    public void initItems() {
//        从duolaimall-shopping拿到所有商品
        List<ItemDto> allItems = productApiClient.getAllProductItem().getCallData();
//        转换为es文档
        List<ItemDocument> itemDocuments = productConverter.items2Documnets(allItems);
//        保存索引
        productRepository.saveAll(itemDocuments);
    }
}
